package rpc.utils;

import io.netty.util.internal.StringUtil;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import rpc.bootstrap.spring.ServiceBean;
import rpc.core.FlowInfo;

/*********
 *
 *
 *@author 197
 *@date 2020/6/23 8:26
 *
 **/


public class ServiceBean2FlowInfo {

  public static Map<String, FlowInfo> convert(ServiceBean serviceBean) {
    Map<String, FlowInfo> infoHashSet = new HashMap<>();
    Class<?> aClass = ReflectionUtils.getClass(serviceBean.getInterfaceName());
    String className = StringUtil.simpleClassName(aClass).toLowerCase();
    Method[] methods = aClass.getMethods();
    FlowInfo flowInfo;
    for (Method method : methods) {
      flowInfo = new FlowInfo().setFlowName(className + "." + method.getName())
          .setSmId(ReflectionUtils.getFliedIntValue(aClass, "serviceId") + "-" + ReflectionUtils
              .getFliedIntValue(aClass, method.getName() + "_MsgId"))
          .setMethod(method).setReqClass(method.getParameterTypes()[0])
          .setRetClass(method.getReturnType());
      infoHashSet.put(flowInfo.getSmId(), flowInfo);
    }

    return infoHashSet;
  }
}
